//
//  NSBundle+TYProduct.m
//  TYFoundation
//
//  Created by 郭艺伟 on 2019/2/28.
//

#import "NSBundle+TYProduct.h"

@implementation NSBundle (TYProduct)

- (NSString *)ty_shortVersionString {
    return [self objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}

- (NSString *)ty_versionString {
    return [self objectForInfoDictionaryKey:@"CFBundleVersion"];
}

- (NSString *)ty_displayNameString {
    NSString *strAppName = NSLocalizedStringFromTable(@"CFBundleDisplayName", @"InfoPlist", nil);
    if (strAppName && [strAppName length] > 0 && ![strAppName isEqualToString:@"CFBundleDisplayName"]) {
        return strAppName;
    }
    
    strAppName = [self objectForInfoDictionaryKey:@"CFBundleDisplayName"];
    if (strAppName && [strAppName length] > 0) {
        return strAppName;
    }
    
    strAppName = NSLocalizedStringFromTable(@"CFBundleName", @"InfoPlist", nil);
    if (strAppName && [strAppName length] > 0 && ![strAppName isEqualToString:@"CFBundleName"]) {
        return strAppName;
    }
    
    return [self objectForInfoDictionaryKey:@"CFBundleName"];
}

// 版本号格式为 1.1.0 或 1.1.0.1 （四位数）,每位的数字不能超过99
- (NSInteger)ty_applicationVersionInt {
    NSString *version = [self ty_shortVersionString];
    if (!version) {
        return 0;
    }
    
    NSArray *splitArray = [version componentsSeparatedByString:@"."];
    __block NSInteger result = 0;
    [splitArray enumerateObjectsUsingBlock:^(NSNumber *obj, NSUInteger idx, BOOL *stop) {
        result = result * 100 + [obj integerValue];
    }];
    
    while (result < 1000000) {
        result *= 100;
    }
    
    return result;
}

@end
